#pragma once

#include "Structure.h"

class Database;

struct StringArray
{
	CStringArray ary;
	bool isUpdated;

	StringArray():isUpdated(false){}
	StringArray(const StringArray &rhs):isUpdated(rhs.isUpdated){ary.Copy(rhs.ary);}
	StringArray& operator=(const StringArray& rhs){ary.Copy(rhs.ary);isUpdated=rhs.isUpdated;return *this;}
};

class FileManagement
{
public:
	FileManagement();
	~FileManagement();

	CNamesPtrArray& GetAllLabels();
	CNamesPtrArray& GetAllLibrary();
	CStringArray& GetLabelClass();
	
	void GetFilesFromLabel(LPCTSTR labelName,CStringArray& files);
	void GetLabelsFromFile(LPCTSTR fileName,CStringArray& labelsOfFile);
	bool GetFilesFromRule(LPCTSTR rule,CStringArray& ary);

	bool AddLabel(CString& label, CString& errorMessage);
	bool AddLabel(CString& label, CString& labelclass, CString& errorMessage);

	bool AddLabelClass(CString& labelclass, CString& errorMessage);

	bool AddLibrary(CString& library, CString& rule, CString& errorMessage);
	void RemoveFilePaths(CString& path);

	void DeleteLabel(LPCTSTR label);
	void DeleteLabelClass(LPCTSTR labelclass);
	void DeleteLibrary(LPCTSTR library);

	bool ChangeLabelName(CString& oldName, CString& newName, CString& errorMessage);
	bool ChangeLabelClassName(LPCTSTR oldName, LPCTSTR newName);
	void ChangeLabelClass(LPCTSTR label, LPCTSTR newClass);
	void ChangeLabelClassToNull(LPCTSTR label);
	bool ChangeLibraryName(LPCTSTR oldName, LPCTSTR newName);
	bool ChangeLibraryRule(CString& libraryName, CString& newRule, CString& errorMessage);
	void ChangeFilePath(LPCTSTR oldPath,LPCTSTR newPath);

	void AddLabelToFile(LPCTSTR label, LPCTSTR file);
	void AddLabelsToFiles(CStringArray &labels, CStringArray &files);
	void AddLabelsToFile(CStringArray &labels, LPCTSTR file);
	void AddLabelToFiles(LPCTSTR label, CStringArray &files);

	void RemoveLabelFromFile(LPCTSTR label, LPCTSTR file);

	void GetAllFilePaths(CStringArray &ary);

	bool IsLabelClassExists(LPCTSTR labelClass);
	bool IsLabelExists(LPCTSTR label);
	bool IsLibraryExists(LPCTSTR library);
	bool IsLabelNameLegal(const CString& name);
private:
	FileManagement(const FileManagement&);
	FileManagement& operator=(const FileManagement&);
	Database* db;

	bool isLabelsChanged;
	CNamesPtrArray labels;

	bool isLibrariesChanged;
	CNamesPtrArray libraries;

	bool isLabelClassChanged;
	CStringArray labelClass;

	//CStringArray labelsFromFile;
	CString uncategorizedName;

	CMap<CString,LPCTSTR,StringArray,StringArray&> filesOfLabel;
	CMap<CString,LPCTSTR,StringArray,StringArray&> filesOfLibrary;
};